clear all
set more off

global source 		"S:\Project\DemoSos2\data2020\NPR"
global cleandata 	"S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"

cd "S:\Project\DemoSos2\common\felles\JR_RG\DrVA\CleanData\"

*------------------------------------------------------------------------------*
** SOMATIC **
*------------------------------------------------------------------------------*
foreach num of numlist  2008/2014 {
	use "$source\SOM\somatikk`num'.dta", clear
	destring _all, replace
	drop if lopenr == ""

	* Diagnoses:
	ge icd_l = substr(hoved1,1,1)
	drop hoved2
	*ge icd_l2 = substr(hoved2,1,1)
	*tab1 icd_l icd_l2
	replace icd_l = "" if icd_l == "9"
	*tab1 icd_l icd_l2

	tab  behandlingsniva3, m
	* Da blir outpatient kun poliklinikk
	gen admtype = 1 if behandlingsniva3 == 1 | behandlingsniva3 == 2
	replace admtype = 2 if behandlingsniva3 == 3
	lab var admtype "1 = inpatient (døgn+dagbeh.); 2 = outpatient (poliklinikk)"

	*** Indikatorer for i registeret (at all/inpatient/outpatient)
	ge npr = 1
	* split scheduled adm in in- and outpatient groups
	ge npr_in = 1 if admtype == 1 & innmatehast == 2
	ge npr_out = 1 if admtype == 2 & innmatehast == 2
	* Emergency adm
	ge npr_er= 1 if innmatehast == 1

	*** Ekskluderer preg. related stuff
	* Alle O'er er svangerskap etc
	* Z30-Z39 (Z3A) har med reproduksjonssprsmål o.l. 
	gen tmp1 = ( (regexm(hoved1, "O")) | (regexm(hoved1, "Z3")) )

	ge npr_nopreg = npr if tmp1!=1
	ge npr_nopreg_in = npr_in if tmp1!=1
	ge npr_nopreg_out = npr_out if tmp1!=1
	ge npr_nopreg_er = npr_er if tmp1!=1

	rename aar year
	rename liggetid npr_days

	gen main1 = substr(hoved1,1,1)
	*gen main2 = substr(hoved2,1,1)
	gen icd101 = substr(hoved1,1,3)
	*gen icd102 = substr(hoved2,1,3)

	gen main_o1 = substr(hoved1,1,3) if main1== "O"
	*gen main_o2 = substr(hoved2,1,3) if main2== "O"

	gen main_o1n = substr(hoved1,2,3) if main1== "O"
	*gen main_o2n = substr(hoved2,2,3) if main1== "O"
	*replace main_o2n = "" if main_o2n=="8n0" 
	destring main_o1n, replace
	replace main_o1n = int(main_o1n/10)
	*replace main_o2n = int(main_o2n/10)

	tempfile data
	save `data'

	charlson hoved1 if main1 != "O", index(10) idvar(lopenr) 
	merge 1:m lopenr using `data'

	ge npr_inf = main1 == "A" 
	ge npr_cancer_c = main1 == "C" 
	ge npr_cancer_d = main1 == "D" 
	ge npr_endocr = main1 == "E" 
	ge npr_mental = main1 == "F"
	ge npr_nervous = main1 == "G" 
	ge npr_ear_eye = main1 == "H" 
	ge npr_heart = main1 == "I" 
	ge npr_resp = main1 == "J" 
	ge npr_disgest = main1 == "K" 
	ge npr_skin = main1 == "L"
	ge npr_muscle = main1 == "M" 
	ge npr_gen = main1 == "N"

	ge npr_obes = main1 == "O" 
	ge npr_preg_comp = (main1== "O" & main_o1n >= 30 & main_o1n <= 48) 
	ge npr_preg_del = (main1== "O" & main_o1n >= 80 & main_o1n <= 84) 
	ge npr_preg_lab = (main1== "O" & main_o1n >= 85 & main_o1n <= 92)
	ge npr_preg_oth = npr_obes==1 & (npr_preg_comp!=1 & npr_preg_del!=1 & npr_preg_lab !=1)
	
	* deadly heart conditions
	ge npr_ami = icd101 =="I21"
	ge npr_chronic_isch = icd101 == "I25"
	ge npr_hfailure = icd101 == "I50"
	ge npr_stroke = icd101 == "I64"
	
	ge npr_cong = main1 == "Q" 
	ge npr_other = main1 == "S" | main1 == "T" | main1 == "V"  | main1 == "W" | main1 == "X" | main1 == "Y" 
	ge npr_injuries = main1 =="S" |main1 == "T"
	ge npr_accident = main1 =="V" |main1 == "W"
	ge npr_other_ext = main1 =="X"
	ge npr_rest = main1 == "R"  | main1 == "Z" 
	
	* medical screenings
	ge npr_genex = (main1== "Z" & main_o1n >= 00 & main_o1n <= 13) 
	ge npr_comex = (main1== "Z" & main_o1n >= 20 & main_o1n <= 29) 
	ge npr_reprex = (main1== "Z" & main_o1n >= 30 & main_o1n <= 39) 
	ge npr_spex = (main1== "Z" & main_o1n >= 40 & main_o1n <= 49) 
	ge npr_emoex = (main1== "Z" & main_o1n >= 50 & main_o1n <= 76) 
	ge npr_famex = (main1== "Z" & main_o1n >= 80 & main_o1n <= 99)
	ge npr_cancerex = icd101 == "Z08" | icd101 == "Z12" | icd101 == "Z80" | icd101 == "Z85" 
	ge npr_chemo = hoved1 == "Z510" | hoved1 == "Z511" | hoved1 == "Z512" 

	ge treat = 1 if icd101 == "A15" | icd101 == "A16" | icd101 == "A17" | icd101 == "A18" | icd101 == "A19" | icd101 == "B90" | icd101 == "C53" | icd101 == "I05" | icd101 == "I06" | icd101 == "I07"| icd101 == "I08" | icd101 == "I09" | main1=="J" | icd101 == "K35" | icd101 == "K36" | icd101 == "K37" | icd101 == "K38" | icd101 == "K40" | icd101 == "K41" | icd101 == "K42" | icd101 == "K43" | icd101 == "K44" | icd101 == "K45" | icd101 == "K46" | icd101 == "I10" | icd101 == "I11" | icd101 == "I12" | icd101 == "I13" | icd101 == "I14"  | icd101 == "I15" | icd101 == "I60" | icd101 == "I61" | icd101 == "I62"| icd101 == "I63"| icd101 == "I64"| icd101 == "I65" | icd101 == "I66" | icd101 == "I67"| icd101 == "I68" | icd101 == "I69" | icd101 == "K80" | icd101 == "K81"

	ge notreat = 1 if treat==.
	collapse (sum) npr* treat (mean) charlindex , by(lopenr)
	ge year = `num'
	save "npr`num'.dta", replace
}



foreach num of numlist  2015/2020 {
	use "$source\SOM\somatikk`num'.dta", clear
	replace aktivitetskategori3  = "" if aktivitetskategori3=="7gesund"
	destring _all, replace
	drop if lopenr == ""

	* Diagnoses:
	ge icd_l = substr(hoved1,1,1)
	drop hoved2
	*ge icd_l2 = substr(hoved2,1,1)
	*tab1 icd_l icd_l2
	replace icd_l = "" if icd_l == "9"
	*tab1 icd_l icd_l2

	tab  behandlingsniva3, m
	* Da blir outpatient kun poliklinikk
	gen admtype = 1 if aktivitetskategori3 == 1 | aktivitetskategori3 == 2
	replace admtype = 2 if aktivitetskategori3 == 3
	lab var admtype "1 = inpatient (døgn+dagbeh.); 2 = outpatient (poliklinikk)"

	*** Indikatorer for i registeret (at all/inpatient/outpatient)
	ge npr = 1
	* split scheduled adm in in- and outpatient groups
	ge npr_in = 1 if admtype == 1 & innmatehast == 2
	ge npr_out = 1 if admtype == 2 & innmatehast == 2
	* Emergency adm
	ge npr_er= 1 if innmatehast == 1

	*** Ekskluderer preg. related stuff
	* Alle O'er er svangerskap etc
	* Z30-Z39 (Z3A) har med reproduksjonssprsmål o.l. 
	gen tmp1 = ( (regexm(hoved1, "O")) | (regexm(hoved1, "Z3")) )

	ge npr_nopreg = npr if tmp1!=1
	ge npr_nopreg_in = npr_in if tmp1!=1
	ge npr_nopreg_out = npr_out if tmp1!=1
	ge npr_nopreg_er = npr_er if tmp1!=1

	rename aar year
	rename liggetid npr_days

	gen main1 = substr(hoved1,1,1)
	*gen main2 = substr(hoved2,1,1)
	gen icd101 = substr(hoved1,1,3)
	*gen icd102 = substr(hoved2,1,3)

	gen main_o1 = substr(hoved1,1,3) if main1== "O"
	*gen main_o2 = substr(hoved2,1,3) if main2== "O"

	gen main_o1n = substr(hoved1,2,3) if main1== "O"
	*gen main_o2n = substr(hoved2,2,3) if main1== "O"
	*replace main_o2n = "" if main_o2n=="8n0" 
	destring main_o1n, replace
	replace main_o1n = int(main_o1n/10)
	*replace main_o2n = int(main_o2n/10)

	tempfile data
	save `data'

	charlson hoved1 if main1 != "O", index(10) idvar(lopenr) 
	merge 1:m lopenr using `data'

	ge npr_inf = main1 == "A" 
	ge npr_cancer_c = main1 == "C" 
	ge npr_cancer_d = main1 == "D"  
	ge npr_endocr = main1 == "E" 
	ge npr_mental = main1 == "F"
	ge npr_nervous = main1 == "G" 
	ge npr_ear_eye = main1 == "H" 
	ge npr_heart = main1 == "I" 
	ge npr_resp = main1 == "J" 
	ge npr_disgest = main1 == "K" 
	ge npr_skin = main1 == "L"
	ge npr_muscle = main1 == "M" 
	ge npr_gen = main1 == "N"

	ge npr_obes = main1 == "O" 
	ge npr_preg_comp = (main1== "O" & main_o1n >= 30 & main_o1n <= 48) 
	ge npr_preg_del = (main1== "O" & main_o1n >= 80 & main_o1n <= 84) 
	ge npr_preg_lab = (main1== "O" & main_o1n >= 85 & main_o1n <= 92)
	ge npr_preg_oth = npr_obes==1 & (npr_preg_comp!=1 & npr_preg_del!=1 & npr_preg_lab !=1)
	
	* deadly heart conditions
	ge npr_ami = icd101 =="I21"
	ge npr_chronic_isch = icd101 == "I25"
	ge npr_hfailure = icd101 == "I50"
	ge npr_stroke = icd101 == "I64"
	
	ge npr_cong = main1 == "Q" 
	ge npr_other = main1 == "S" | main1 == "T" | main1 == "V"  | main1 == "W" | main1 == "X" | main1 == "Y" 
	ge npr_injuries = main1 =="S" |main1 == "T"
	ge npr_accident = main1 =="V" |main1 == "W"
	ge npr_other_ext = main1 =="X"
	ge npr_rest = main1 == "R"  | main1 == "Z" 
	
	* medical screenings
	ge npr_genex = (main1== "Z" & main_o1n >= 00 & main_o1n <= 13) 
	ge npr_comex = (main1== "Z" & main_o1n >= 20 & main_o1n <= 29) 
	ge npr_reprex = (main1== "Z" & main_o1n >= 30 & main_o1n <= 39) 
	ge npr_spex = (main1== "Z" & main_o1n >= 40 & main_o1n <= 49) 
	ge npr_emoex = (main1== "Z" & main_o1n >= 50 & main_o1n <= 76) 
	ge npr_famex = (main1== "Z" & main_o1n >= 80 & main_o1n <= 99)
	ge npr_cancerex = icd101 == "Z08" | icd101 == "Z12" | icd101 == "Z80" | icd101 == "Z85" 
	ge npr_chemo = hoved1 == "Z510" | hoved1 == "Z511" | hoved1 == "Z512" 

	ge treat = 1 if icd101 == "A15" | icd101 == "A16" | icd101 == "A17" | icd101 == "A18" | icd101 == "A19" | icd101 == "B90" | icd101 == "C53" | icd101 == "I05" | icd101 == "I06" | icd101 == "I07"| icd101 == "I08" | icd101 == "I09" | main1=="J" | icd101 == "K35" | icd101 == "K36" | icd101 == "K37" | icd101 == "K38" | icd101 == "K40" | icd101 == "K41" | icd101 == "K42" | icd101 == "K43" | icd101 == "K44" | icd101 == "K45" | icd101 == "K46" | icd101 == "I10" | icd101 == "I11" | icd101 == "I12" | icd101 == "I13" | icd101 == "I14"  | icd101 == "I15" | icd101 == "I60" | icd101 == "I61" | icd101 == "I62"| icd101 == "I63"| icd101 == "I64"| icd101 == "I65" | icd101 == "I66" | icd101 == "I67"| icd101 == "I68" | icd101 == "I69" | icd101 == "K80" | icd101 == "K81"

	ge notreat = 1 if treat==.
	collapse (sum) npr* treat (mean) charlindex , by(lopenr)
	ge year = `num'
	save "npr`num'.dta", replace
}


use "npr2008.dta", clear
for num 2009/2020: append using nprX
ge  npr_noer =  npr_out + npr_in
save npr, replace
for num 2008/2020: erase nprX.dta


